package com.hxkj.controller.article;

import com.alibaba.fastjson.JSONObject;
import com.hxkj.domain.po.Article;
import com.hxkj.service.article.ArticleService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.Callable;

/**
 * 文章控制层
 * <p>
 * Created by zzx on 2018/6/6.
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 获取文章列表（包含文章回收站列表、文章待审核列表、文章待发布列表、文章已发布列表）
     *
     * @param article pageParam
     * @return 翻页数据
     * @throws Exception
     */
    @RequestMapping(value = "/getlist", method = RequestMethod.POST)
    public Callable<ResultPageStruct<Article>> getArticleList(@ModelAttribute Article article, @ModelAttribute PageTo pageParam) throws Exception {
        return () -> articleService.getArticleList(article == null ? new Article() : article, pageParam);
    }


    /**
     * 获取一条文章信息
     *
     * @param article
     * @return 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Callable<Article> getArticle(@ModelAttribute Article article) throws Exception {
        return () -> articleService.getArticle(article);
    }

    /**
     * 发布一篇文章
     *
     * @param jsonObject 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public void saveArticle(@RequestBody JSONObject jsonObject) throws Exception {
        articleService.saveArticle(jsonObject);
    }

    /**
     * 移除文章至回收站
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public void removerArticle(@ModelAttribute Article article) throws Exception {
        articleService.removerArticle(article);
    }

    /**
     * 回收站 删除文章
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void deleteArticle(@ModelAttribute Article article) throws Exception {
        articleService.deleteArticle(article);
    }

    /**
     * 修改文章  (每次修改单条文章)
     *
     * @param jsonObject 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public void updateArticle(@RequestBody JSONObject jsonObject) throws Exception {
        articleService.modifyArticle(jsonObject);
    }

    /**
     * 文章恢复
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/articlerecover", method = RequestMethod.POST)
    public void articleRecover(@ModelAttribute Article article) throws Exception {
        articleService.articleRecover(article);
    }

    /**
     * 文章审核/设置文章推荐级别  (文章列表底部,可勾选,传的是articleIds)
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void articleCheck(@ModelAttribute Article article) throws Exception {
        articleService.updateArticle(article);
    }

    /**
     * 文章置顶
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/stick", method = RequestMethod.POST)
    public void articleStick(@ModelAttribute Article article) throws Exception {
        articleService.articleStick(article);
    }

    /**
     * 保存曝光或咨询
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/saveright", method = RequestMethod.POST)
    public void saveRight(@ModelAttribute Article article, String msg) throws Exception {
        articleService.saveRight(article, msg);
    }

    /**
     * 获取曝光或咨询的总条数
     *
     * @param article 文章信息
     * @throws Exception
     */
    @RequestMapping(value = "/getrightscount", method = RequestMethod.POST)
    public Integer getRightsCount(@ModelAttribute Article article) throws Exception {
        return articleService.getRightsCount(article);
    }

    /**
     * @return com.platform.common.domain.result.ResultDataStruct<com.hxkj.domain.po.Article>
     * @author Mr.zheng
     * @Description modifyHeat 根据文章ID和站点ID修改文章热度值
     * @Date 9:31 2018/8/9
     * @Param [article]
     **/
    @RequestMapping(value = "/modifyHeat", method = RequestMethod.POST)
    public Callable<Article> modifyHeat(@ModelAttribute Article article) throws Exception {
        return () -> articleService.modifyHeat(article);
    }
}
